<?php session_start(); //Session ?>
<?php include_once( "_db.php" ); //Database ?>
<?php include_once( "funcdef.php" ); //Functions Definition ?>
<?php include_once( "header.php" ); //Header ?>
<?php include_once( "footer.php" ); //Footer ?>
<?php
//include page header
header_template("Order", $_SERVER['PHP_SELF'], "icon-file");

//db OBJECT invoked!
$db = new DATABASE();

?>
      <div class="row">
        <div class="span6">
<?php
  //Get Deal
  $query1 = "SELECT COUNT(deal.deal_id) FROM deal";
  $query2 = "SELECT * FROM deal";
  if (isset($_GET["status"])) {
    if ($_GET["status"] == "all") {
      $query1 = "SELECT COUNT(deal.deal_id) FROM deal";
      $query2 = "SELECT * FROM deal";
    }
    if ($_GET["status"] == "billed") {
      if (($_GET["bill"] == "all") || (!isset($_GET["bill"]))) {
        $query1 = "SELECT COUNT(deal.deal_id) FROM deal INNER JOIN bill USING (deal_id)";
        $query2 = "SELECT * FROM deal INNER JOIN bill USING (deal_id)";
      } elseif ($_GET["bill"] == "new") {
        $query1 = "SELECT COUNT(deal.deal_id) FROM deal INNER JOIN bill USING (deal_id) WHERE bill.bill_status LIKE 'new'";
        $query2 = "SELECT * FROM deal INNER JOIN bill USING (deal_id) WHERE bill.bill_status LIKE 'new'";
      } elseif ($_GET["bill"] == "completed") {
        $query1 = "SELECT COUNT(deal.deal_id) FROM deal INNER JOIN bill USING (deal_id) WHERE bill.bill_status LIKE 'completed'";
        $query2 = "SELECT * FROM deal INNER JOIN bill USING (deal_id) WHERE bill.bill_status LIKE 'completed'";
      } elseif ($_GET["bill"] == "pending") {
        $query1 = "SELECT COUNT(deal.deal_id) FROM deal INNER JOIN bill USING (deal_id) WHERE bill.bill_status LIKE 'pending'";
        $query2 = "SELECT * FROM deal INNER JOIN bill USING (deal_id) WHERE bill.bill_status LIKE 'pending'";
      } elseif ($_GET["bill"] == "canceled") {
        $query1 = "SELECT COUNT(deal.deal_id) FROM deal INNER JOIN bill USING (deal_id) WHERE bill.bill_status LIKE 'canceled";
        $query2 = "SELECT * FROM deal INNER JOIN bill USING (deal_id) WHERE bill.bill_status LIKE 'canceled'";
      } elseif ($_GET["bill"] == "expired") {
        $query1 = "SELECT COUNT(deal.deal_id) FROM deal INNER JOIN bill USING (deal_id) WHERE bill.bill_status LIKE 'expired'";
        $query2 = "SELECT * FROM deal INNER JOIN bill USING (deal_id) WHERE bill.bill_status LIKE 'expired'";
      } else {
        $query1 = "SELECT COUNT(deal.deal_id) FROM deal INNER JOIN bill USING (deal_id)";
        $query2 = "SELECT * FROM deal INNER JOIN bill USING (deal_id)";
      }
    }
    if ($_GET["status"] == "paid") {
      $query1 = "SELECT COUNT(deal.deal_id) FROM deal INNER JOIN pay USING (deal_id)";
      $query2 = "SELECT * FROM deal INNER JOIN pay USING (deal_id)";
    }
    if ($_GET["status"] == "shipped") {
      if (($_GET["ship"] == "all") || (!isset($_GET["ship"]))) {
        $query1 = "SELECT COUNT(deal.deal_id) FROM deal INNER JOIN ship USING (deal_id)";
        $query2 = "SELECT * FROM deal INNER JOIN ship USING (deal_id)";
      } elseif ($_GET["ship"] == "new") {
        $query1 = "SELECT COUNT(deal.deal_id) FROM deal INNER JOIN ship USING (deal_id) WHERE ship.ship_status LIKE 'new'";
        $query2 = "SELECT * FROM deal INNER JOIN ship USING (deal_id) WHERE ship.ship_status LIKE 'new'";
      } elseif ($_GET["ship"] == "packing") {
        $query1 = "SELECT COUNT(deal.deal_id) FROM deal INNER JOIN ship USING (deal_id) WHERE ship.ship_status LIKE 'packing'";
        $query2 = "SELECT * FROM deal INNER JOIN ship USING (deal_id) WHERE ship.ship_status LIKE 'packing'";
      } elseif ($_GET["ship"] == "shipped") {
        $query1 = "SELECT COUNT(deal.deal_id) FROM deal INNER JOIN ship USING (deal_id) WHERE ship.ship_status LIKE 'completed'";
        $query2 = "SELECT * FROM deal INNER JOIN ship USING (deal_id) WHERE ship.ship_status LIKE 'completed'";
      } else {
        $query1 = "SELECT COUNT(deal.deal_id) FROM deal INNER JOIN ship USING (deal_id)";
        $query2 = "SELECT * FROM deal INNER JOIN ship USING (deal_id)";
      }
    }
  }
?>
          <p>
            <div class="clearfix btn-group">
              <a href="<?php echo $_SERVER['PHP_SELF']."?status=all"; ?>" class="btn btn-mini btn-info">All</a>
              <a href="<?php echo $_SERVER['PHP_SELF']."?status=billed"; ?>" class="btn btn-mini btn-info">Billed</a>
              <a href="<?php echo $_SERVER['PHP_SELF']."?status=paid"; ?>" class="btn btn-mini btn-info">Paid</a>
              <a href="<?php echo $_SERVER['PHP_SELF']."?status=shipped"; ?>" class="btn btn-mini btn-info">Shipped</a>
            </div>
          </p>
          <?php if ($_GET["status"] == "billed") { ?>
          <p>
            <div class="clearfix btn-group">
              <a href="<?php echo $_SERVER['PHP_SELF']."?status=billed&bill=all"; ?>" class="btn btn-mini btn-info">All</a>
              <a href="<?php echo $_SERVER['PHP_SELF']."?status=billed&bill=new"; ?>" class="btn btn-mini btn-info">New</a>
              <a href="<?php echo $_SERVER['PHP_SELF']."?status=billed&bill=completed"; ?>" class="btn btn-mini btn-info">Paid</a>
              <a href="<?php echo $_SERVER['PHP_SELF']."?status=billed&bill=pending"; ?>" class="btn btn-mini btn-info">Pending</a>
              <a href="<?php echo $_SERVER['PHP_SELF']."?status=billed&bill=canceled"; ?>" class="btn btn-mini btn-info">Canceled</a>
              <a href="<?php echo $_SERVER['PHP_SELF']."?status=billed&bill=expired"; ?>" class="btn btn-mini btn-info">Expired</a>
            </div>
          </p>
          <?php } ?>
          <?php if ($_GET["status"] == "shipped") { ?>
          <p>
            <div class="clearfix btn-group">
              <a href="<?php echo $_SERVER['PHP_SELF']."?status=shipped&ship=all"; ?>" class="btn btn-mini btn-info">All</a>
              <a href="<?php echo $_SERVER['PHP_SELF']."?status=shipped&ship=new"; ?>" class="btn btn-mini btn-info">New</a>
              <a href="<?php echo $_SERVER['PHP_SELF']."?status=shipped&ship=packing"; ?>" class="btn btn-mini btn-info">Packing</a>
              <a href="<?php echo $_SERVER['PHP_SELF']."?status=shipped&ship=shipped"; ?>" class="btn btn-mini btn-info">Shipped</a>
            </div>
          </p>
          <?php } ?>
<?php
//Pagination
//set number of deal / page
$dealPerPage = 10;
//initialize current page
$curPage = 0;
//set first page
$firstPage = 1;
//initialize last page
$rowCount = $db->database->query($query1);
$rowCount = $rowCount->fetchAll(SQLITE_NUM);
$rowCount = intval($rowCount[0][0]);
$remainder = intval($rowCount % $dealPerPage);
$lastPage = intval($rowCount / $dealPerPage);
//set current page
if ((isset($_GET["page"])) && (is_numeric($_GET["page"]))) {
  $curPage = intval($_GET["page"]);
} else {
  $curPage = $firstPage;
}
//set last page
if ($remainder > 0) {
  $lastPage++;
}
if ($lastPage > 1) { //pagination start
?>
          <ul class="pager">
<?php

  $pager_extend_href = "";
  if (($_GET["status"] == "billed") && (isset($_GET["bill"]))) {
    $pager_extend_href = "&bill=".$_GET["bill"];
  }
  if (($_GET["status"] == "shipped") && (isset($_GET["ship"]))) {
    $pager_extend_href = "&ship=".$_GET["ship"];
  }
  
  if (isset($_GET["status"])) {
    $pager_deal_status = "&status=".$_GET["status"];
  } else {
    $pager_deal_status = "";
  }
  if ($curPage != $firstPage) {
    $pager_prev_state = " active";
    $pager_prev_href  = " href=\"".$_SERVER['PHP_SELF']."?page=".($curPage-1).$pager_deal_status.$pager_extend_href."\"";
  } else {
    $pager_prev_state = " disabled";
    $pager_prev_href  = "";
  }

  if ($curPage != $lastPage) {
    $pager_next_state = " active";
    $pager_next_href  = " href=\"".$_SERVER['PHP_SELF']."?page=".($curPage+1).$pager_deal_status.$pager_extend_href."\"";
  } else {
    $pager_next_state = " disabled";
    $pager_next_href  = "";
  }
?>
            <li class="previous<?php echo $pager_prev_state; ?>">
              <a<?php echo $pager_prev_href; ?>>&larr; Prev</a>
            </li>
            <li class="next<?php echo $pager_next_state; ?>">
              <a<?php echo $pager_next_href; ?>>Next &rarr;</a>
            </li>
          </ul>
          <center>
            <p><span class="label label-info">Page <?php echo $curPage; ?>/<?php echo $lastPage; ?></span></p>
          </center>
<?php } //pagination end ?>
<?php
  $result = $db->database->query($query2." ORDER BY deal.deal_id DESC LIMIT ".(($curPage-1)*$dealPerPage).", ".$dealPerPage);
  $tmpDeal = $result->fetchAll(SQLITE_ASSOC);
?>
          <table class="table table-condensed table-hover">
            <thead>
              <tr>
                <th>Transaction #</th>
                <th>Date</th>
                <th>Status</th>
                <td>&nbsp;</td>
              </tr>
            </thead>
            <tbody>
<?php
  if (!empty($tmpDeal)) {
    foreach ($tmpDeal as $deal) {
      if (!strpos($query2, "JOIN")) {
        $deal_id = $deal['deal_id'];
        $deal_timestamp = $deal['deal_timestamp'];
        $deal_status = $deal['deal_status'];
        $customer_id = $deal['customer_id'];
        $address_id = $deal['address_id'];
      } else {
        $deal_id = $deal['deal.deal_id'];
        $deal_timestamp = $deal['deal.deal_timestamp'];
        $deal_status = $deal['deal.deal_status'];
        $customer_id = $deal['deal.customer_id'];
        $address_id = $deal['deal.address_id'];
      }

      $status_lbl_text = "new";
      $status_lbl_color = "";
      if ($deal_status == "billed") {
        $status_lbl_text = "billed";
        $status_lbl_color = " label-info";
      }
      if ($deal_status == "paid") {
        $status_lbl_text = "paid";
        $status_lbl_color = " label-success";
      }
      if ($deal_status == "shipped") {
        $status_lbl_text = "shipped";
        $status_lbl_color = " label-inverse";
      }
      //Get Customer
      $cols = array();
      $cols = array(1);
      $conds = array();
      $conds[] = array(0);
      $conds[] = array(0);
      $conds[] = array($customer_id);
      $query = $db->db_select("customer", $cols, $conds);
      $queries[] .= $query.";";
      $result = $db->database->query($query);
      $tmpCust = $result->fetchAll(SQLITE_ASSOC);
      $deal_view_href = "transaction.php?dealid=".$deal_id."&email=".$tmpCust[0]['customer_email'];

      if ($_GET["status"] == "billed") {
        if ($deal['bill.bill_status'] == "new") {
          $status_lbl_text2 = "new";
          $status_lbl_color2 = " label-default";
        } elseif ($deal['bill.bill_status'] == "completed") {
          $status_lbl_text2 = "paid";
          $status_lbl_color2 = " label-success";
        } elseif ($deal['bill.bill_status'] == "pending") {
          $status_lbl_text2 = "pending";
          $status_lbl_color2 = " label-warning";
        } elseif ($deal['bill.bill_status'] == "canceled") {
          $status_lbl_text2 = "canceled";
          $status_lbl_color2 = " label-important";
        } elseif ($deal['bill.bill_status'] == "expired") {
          $status_lbl_text2 = "expired";
          $status_lbl_color2 = " label-important";
        } else {
          $status_lbl_text2 = "";
          $status_lbl_color2 = "";
        }
      }
      
      if ($_GET["status"] == "shipped") {
        if ($deal['ship.ship_status'] == "new") {
          $status_lbl_text2 = "new";
          $status_lbl_color2 = " label-default";
        } elseif ($deal['ship.ship_status'] == "packing") {
          $status_lbl_text2 = "packing";
          $status_lbl_color2 = " label-warning";
        } elseif ($deal['ship.ship_status'] == "completed") {
          $status_lbl_text2 = "shipped";
          $status_lbl_color2 = " label-success";
        } else {
          $status_lbl_text2 = "";
          $status_lbl_color2 = "";
        }
      }
?>
              <tr>
                <td><small><?php echo $deal_id; ?></small></td>
                <td><small><?php echo date("Y-m-d H:i:s", $deal_timestamp); ?></small><?php echo printTimeZone(); ?></td>
                <td>
                  <small>
                    <?php if (($_GET["status"] == "billed") || ($_GET["status"] == "shipped")) { ?>
                    <span class="label<?php echo $status_lbl_color2; ?>"><?php echo $status_lbl_text2; ?></span>  
                    <?php } else { ?>                    
                    <span class="label<?php echo $status_lbl_color; ?>"><?php echo $status_lbl_text; ?></span>
                    <?php } ?>
                  </small>
                </td>
                <td><a class="btn btn-default btn-mini" href="<?php echo $deal_view_href; ?>">View</a></td>
              </tr>
<?php
    }
  }
?>
            </tbody>
          </table>
        </div>
      </div>
<?php
//include page footer
footer_template();
?>